package lol.niconico.libtest.http

import android.util.Log
import okhttp3.*
import okhttp3.mockwebserver.MockWebServer
import okio.ByteString
import java.util.concurrent.TimeUnit

/**
 * Created by ZhangQianqian on 2020/7/9  17:11.
 * email 415692689@qq.com
 */
object Client {

    lateinit var webSocket: WebSocket

    fun startClient(webSocketUrl: String) {
        val mClient = OkHttpClient.Builder()
            .pingInterval(10, TimeUnit.SECONDS)
            .build()
        val request = Request.Builder()
            .url(webSocketUrl)
            .build()

        webSocket = mClient.newWebSocket(request, object : WebSocketListener() {
            private val TAG = "Client WsListener"

            override fun onMessage(webSocket: WebSocket, text: String) {
                super.onMessage(webSocket, text)
                Log.e(TAG, "客户端收到消息:$text")
            }

            override fun onOpen(webSocket: WebSocket, response: Response) {
                super.onOpen(webSocket, response)
                Log.e(TAG, "连接成功！")
            }
        })
    }
}